﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace Batman.Cms.Core.Core.Attributes
{
    public class AcceessOriginalAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var request = filterContext.HttpContext.Request;
            var response = filterContext.HttpContext.Response;

            var header = response.Headers;
            var requestHeader = request.Headers;
            var orgin = requestHeader["Origin"];
            header.Add("Access-Control-Allow-Origin", orgin.ToString() == null ? string.Empty : requestHeader["Origin"].ToString());
            header.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            header.Add("Access-Control-Max-Age", "3600");
            header.Add("Access-Control-Allow-Headers", "*");
            header.Add("Access-Control-Allow-Credentials", "true");

            base.OnActionExecuted(filterContext);
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
        }
    }
}
